package com.ibiaoke.order.service;

import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.order.dao.OrderAddressDao;
import com.ibiaoke.order.entity.OrderAddress;
import com.ibiaoke.order.entity.OrderAddressExample;
import com.ibiaoke.order.entity.vo.OrderAddressVO;
import com.ibiaoke.system.service.AreaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by SuXin on 2017/6/22.
 */
@Service
public class OrderAddressServiceImpl implements OrderAddressService {

    @Autowired
    private OrderAddressDao orderAddressDao;
    @Autowired
    private AreaService areaService;

    /**
     * 保存收货地址信息
     *
     * @param orderAddressVO
     */
    @Override
    public int saveOrderAddress(OrderAddressVO orderAddressVO) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(orderAddressVO, orderAddress);
        orderAddress.setCreateTime(new Date());
        orderAddress.setUpdateTime(new Date());
        orderAddress.setDelStatus((byte) CommonConsts.NO);
        fillAreaName(orderAddress);
        return orderAddressDao.insertSelective(orderAddress);
    }

    private void fillAreaName(OrderAddress orderAddress) {
        String provinceName = areaService.getProvinceName(orderAddress.getProvinceId());
        String cityName = areaService.getCityName(orderAddress.getCityId());
        String districtName = areaService.geDistrictName(orderAddress.getDistrictId());
        orderAddress.setProvinceName(provinceName);
        orderAddress.setCityName(cityName);
        orderAddress.setDistrictName(districtName);
    }

    /**
     * 更改收货地址信息
     *
     * @param orderAddressVO
     */
    @Override
    public int updateOrderAddress(OrderAddressVO orderAddressVO) {
        OrderAddress orderAddress = new OrderAddress();
        AssertUtils.notNull(orderAddressVO.getAddressId(), "地址Id不能为空");
        BeanUtils.copyProperties(orderAddressVO, orderAddress);
        orderAddress.setUpdateTime(new Date());
        fillAreaName(orderAddress);
        return orderAddressDao.updateByPrimaryKeySelective(orderAddress);
    }

    /**
     * 查询地址列表
     *
     * @return
     */
    @Override
    public List<OrderAddress> listOrderAddress(Integer customerId) {
        return orderAddressDao.listOrderAddress(customerId);
    }

    /**
     * 查询单个地址
     *
     * @param addressId
     * @return orderAddress
     */
    @Override
    public OrderAddress getOrderAddress(Integer addressId) {
        AssertUtils.notNull(addressId, "地址Id不能为空");
        OrderAddressExample example = new OrderAddressExample();
        OrderAddressExample.Criteria criteria = example.createCriteria();
        criteria.andAddressIdEqualTo(addressId);
        criteria.andDelStatusEqualTo((byte) CommonConsts.NO);
        OrderAddress orderAddress = orderAddressDao.getByExample(example);
        AssertUtils.notNull(orderAddress, "查不到地址");
        return orderAddress;
    }


    /**
     * 删除收货地址
     * @param addressId */
    @Override
    public int deleteOrderAddress(Integer addressId) {
        AssertUtils.notNull(addressId, "地址Id不能为空");
        return orderAddressDao.delete(addressId);
    }
}
